<?php
class exerciseskill extends Model{
	/**
	 * 添加某道习题下的解题技巧
	 * @param unknown_type $eid
	 * @param unknown_type $sid
	 */
	public function addExerciseSkill($eid,$sid)
	{
		$esExist = $this->reCheck($eid, $sid);
		if ($esExist) {
			$this->addAccount($esExist['es_id']);
		}else{
			$this->insertExerciseSkill($eid, $sid);
		}
	}
	
	public function addAccount($esid){
		$sql = "UPDATE `howdo`.`exercise_skill` SET `skill_account` = `skill_account` + 1 WHERE `exercise_skill`.`es_id` = '".$esid."';";
		return $this->update($sql);
	}
	public function insertExerciseSkill($eid, $sid){
		$sql = "INSERT INTO `howdo`.`exercise_skill` (`es_id`, `ex_id`, `skill_id`, `skill_account`) "
			 . "VALUES "
			 . "(NULL, '".$eid."', '".$sid."', '1');";
		return $this->insert($sql);
	}
	/**
	 * 根据习题ID获取解体技巧
	 * @param unknown $eid
	 */
	public function getSkilllistByExerciseid($eid)
	{
		$sql = "SELECT `exercise_skill`.*,`skill`.`skill_name`,`skill`.`skill_cont` FROM `exercise_skill` 
				LEFT JOIN `skill` ON `exercise_skill`.`skill_id`=`skill`.`skill_id`
				WHERE `ex_id` = '".$eid."'";
		return $this->fetchAll($sql);
	}
	
	/**
	 * 删除某习题下的某个解题技巧
	 * @param unknown_type $eid
	 * @param unknown_type $sid
	 * @return resource
	 */
	public function delExerciseSkill($eid,$sid)
	{
		$sql = "DELETE FROM `exercise_skill` WHERE `ex_id`='".$eid."' AND `skill_id`='".$sid."'";
		return $this->del($sql);
	}
	
	/**
	 * 根据skillID获取解体技巧
	 * @param unknown $eid
	 */
	public function getSkillByid($sid)
	{
		$sql = "SELECT `skill_cont` FROM `skill` WHERE `skill_id` = $sid";
// 		echo $sql;
		return $this->fetchRow($sql);
	}
	
	public function reCheck($eid, $sid){
		$sql = "SELECT * FROM `exercise_skill` WHERE `ex_id` = '".$eid."' AND `skill_id` = '".$sid."' ";
		return $this->fetchRow($sql);
	}
	
	public function getParentId($sid){
		$sql = "SELECT * FROM `skill` WHERE `skill_id` = '".$sid."'";
		return $this->fetchRow($sql);
	}
}